Let's convert the array `[4, 10, 3, 5, 1]` into a valid max-heap using the `heapify` algorithm.
- The array has 5 elements (\(n=5\)). The last non-leaf node is at index \(\lfloor 5 / 2 \rfloor - 1 = 1\).
- Processing index 1 (value 10): We compare it with its children (5 and 1). Since 10 is the largest, no swap is needed. The subtree is valid.
- Processing index 0 (value 4): We compare it with its children (10 and 3). The largest child is 10. Since \(4 < 10\), we must swap them.
- After first swap: The array is `[10, 4, 3, 5, 1]`. The node with value 4 is now at index 1. We must continue the `bubbleDown` process from this new position.
- Continuing bubble down: We compare 4 with its children (5 and 1). The largest child is 5. Since \(4 < 5\), we swap again.
- Final Heap: The final array is `[10, 5, 3, 4, 1]`, which is now a valid max-heap.
Initial state